package com.ruoyi.wx.controller;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.wx.domain.TGoods;
import com.ruoyi.wx.domain.WxUser;
import com.ruoyi.wx.service.ITGoodsService;
import com.ruoyi.wx.service.IWxUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.TOrder;
import com.ruoyi.wx.service.ITOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2020-05-05
 */
@Controller
@RequestMapping("/wx/order")
public class TOrderController extends BaseController {
    private String prefix = "wx/order";

    @Autowired
    private ITOrderService tOrderService;
    @Autowired
    private IWxUserService wxUserService;
    @Autowired
    private ITGoodsService goodsService;

    @RequiresPermissions("wx:order:view")
    @GetMapping()
    public String order() {
        return prefix + "/order";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("wx:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TOrder tOrder) {
        startPage();
        List<TOrder> list = tOrderService.list(new QueryWrapper<>(tOrder));
        list.forEach(order->{
            order.setUserName(wxUserService.getOne(new QueryWrapper<>(new WxUser().setId(order.getUserId()))).getNickName());
            order.setGoodsName(goodsService.getOne(new QueryWrapper<>(new TGoods().setGoodsId(tOrder.getGoodsId()))).getGoodsName());
        });
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("wx:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TOrder tOrder) {
        List<TOrder> list = tOrderService.list(new QueryWrapper<>(tOrder));
        ExcelUtil<TOrder> util = new ExcelUtil<>(TOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("wx:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TOrder tOrder) {
        return toAjax(tOrderService.save(tOrder));
    }

    /**
     * 修改订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        TOrder tOrder = tOrderService.getById(id);
        mmap.put("tOrder", tOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单
     */
    @RequiresPermissions("wx:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TOrder tOrder) {
        return toAjax(tOrderService.updateById(tOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("wx:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tOrderService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }

    @RequestMapping("/send")
    @ResponseBody
    public AjaxResult send(TOrder tOrder){
        tOrder.setOrderStatus("3");
        return toAjax(tOrderService.saveOrUpdate(tOrder));
    }
}
